package kr.saeildamil.androidstudy.ch19;

import kr.saeildamil.androiddforstudy.R;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class LongTimeTest extends Activity {

	int mValue;
	TextView mText;
	ProgressDialog mProgress;
	boolean mQuit;
	UpdateThread mThread;
	

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_longtimetest);
		
		mText = (TextView) findViewById(R.id.ch19_longtimetest_tv);
		
	}
	
	public void mOnClick(View v){
		mValue = 0;
		showDialog(0);
		mQuit = false;
		mThread = new UpdateThread();
		mThread.start();
	}
	
	@Override
	@Deprecated
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 0:
			mProgress = new ProgressDialog(this);
			mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			mProgress.setTitle("Now Updating...");
			mProgress.setMessage("Please Wait...");
			mProgress.setCancelable(false);
			mProgress.setButton("Cance", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int whichButton) {
					mQuit = true;
					dismissDialog(0);
				}
			});
			return mProgress;
		}
		return null;
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(Message msg) {
			mValue = msg.arg1;
			mText.setText(Integer.toString(mValue));
			if(mValue<100){
				mProgress.setProgress(mValue);
			}else{
				mQuit = true;
				dismissDialog(0);
			}
		}
	};
	
	class UpdateThread extends Thread{
		
		@Override
		public void run() {
			while (mQuit==false) {
				mValue++;
				Message msg = mHandler.obtainMessage();
				msg.arg1 = mValue;
				mHandler.sendMessage(msg);
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
				}
			}
		}
	}
	
}
